Mapping the 2017-18 Pa. flu season
This project is the final assignment for the course “Introduction to R for Journalists” taught by Andrew Ba Tran.
We’re looking at the 2017-28 flu season in Pennsylvania by county.
I used a table capture browser extension to grab this data from the Pennsylvania Department of Health
# Import in CSV with total flu cases, create new DF with only total
flu_18<- read_csv("~/Documents/Census_stuff/Pa_flu_cases/PA_final_flu_18.csv")
# Filter down to only total cases and
flu_18<- select(flu_18, County, Total_flu_cases)
datatable(flu_18,extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')))
Since we are trying to look at the flu’s impact on a county-by-county basis, we need to use the CensusAPI package to bring in the popuation stats.
# Looking at the various Apis available
pop_var <- listCensusMetadata(name= "pep/population",
vintage = 2017,
type= "variables")
pop_vars <- listCensusMetadata(name = "pep/population", vintage = 2017,
type = "geography")
#Getting the Census population data
pa_pop<- getCensus("pep/population",
vintage=2017,
vars=c("POP", "GEONAME"),
region="county:*",
regionin = "state:42")
We need to separate the county name from the state name because the flu data is by county only.
pa_pop_2 <- separate(pa_pop,col = GEONAME, "County", " County, Pennsylvania") %>%
select(County,POP)
pa_pop_2$POP <-as.numeric(pa_pop_2$POP)
# Joining the total flu cases with the population data
pa_flu_rate <-full_join(flu_18,pa_pop_2, by="County")
# Adjusting for a flu rate per 100,000 residents
pa_flu_rate$per_capita <- round(pa_flu_rate$Total_flu_cases/pa_flu_rate$POP*100000,0)
When I create these maps for work, we are interested in highlighting the top 15-25 counties that are being hardest hit by the flu. Here’s a look at that in a scatter plot.
Now we need to bring in the county shapefiles to make the map.
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|==== | 7%
|
|===== | 7%
|
|===== | 8%
|
|====== | 9%
|
|====== | 10%
|
|======= | 10%
|
|======= | 11%
|
|======== | 12%
|
|======== | 13%
|
|========= | 13%
|
|========= | 14%
|
|========= | 15%
|
|========== | 15%
|
|========== | 16%
|
|=========== | 16%
|
|=========== | 17%
|
|=========== | 18%
|
|============ | 18%
|
|============ | 19%
|
|============= | 19%
|
|============= | 20%
|
|============= | 21%
|
|============== | 21%
|
|============== | 22%
|
|=============== | 22%
|
|=============== | 23%
|
|=============== | 24%
|
|================ | 24%
|
|================ | 25%
|
|================= | 26%
|
|================= | 27%
|
|================== | 27%
|
|================== | 28%
|
|=================== | 28%
|
|=================== | 29%
|
|=================== | 30%
|
|==================== | 30%
|
|==================== | 31%
|
|===================== | 32%
|
|===================== | 33%
|
|====================== | 33%
|
|====================== | 34%
|
|====================== | 35%
|
|======================= | 35%
|
|======================= | 36%
|
|======================== | 36%
|
|======================== | 37%
|
|======================== | 38%
|
|========================= | 38%
|
|========================= | 39%
|
|========================== | 39%
|
|========================== | 40%
|
|========================== | 41%
|
|=========================== | 41%
|
|=========================== | 42%
|
|============================ | 42%
|
|============================ | 43%
|
|============================ | 44%
|
|============================= | 44%
|
|============================= | 45%
|
|============================== | 45%
|
|============================== | 46%
|
|============================== | 47%
|
|=============================== | 47%
|
|=============================== | 48%
|
|================================ | 48%
|
|================================ | 49%
|
|================================ | 50%
|
|================================= | 50%
|
|================================= | 51%
|
|================================== | 52%
|
|================================== | 53%
|
|=================================== | 53%
|
|=================================== | 54%
|
|=================================== | 55%
|
|==================================== | 55%
|
|==================================== | 56%
|
|===================================== | 56%
|
|===================================== | 57%
|
|===================================== | 58%
|
|====================================== | 58%
|
|====================================== | 59%
|
|======================================= | 59%
|
|======================================= | 60%
|
|======================================= | 61%
|
|======================================== | 61%
|
|======================================== | 62%
|
|========================================= | 62%
|
|========================================= | 63%
|
|========================================= | 64%
|
|========================================== | 64%
|
|========================================== | 65%
|
|=========================================== | 65%
|
|=========================================== | 66%
|
|=========================================== | 67%
|
|============================================ | 67%
|
|============================================ | 68%
|
|============================================= | 69%
|
|============================================= | 70%
|
|============================================== | 70%
|
|============================================== | 71%
|
|============================================== | 72%
|
|=============================================== | 72%
|
|=============================================== | 73%
|
|================================================ | 73%
|
|================================================ | 74%
|
|================================================ | 75%
|
|================================================= | 75%
|
|================================================= | 76%
|
|================================================== | 76%
|
|================================================== | 77%
|
|================================================== | 78%
|
|=================================================== | 78%
|
|=================================================== | 79%
|
|==================================================== | 79%
|
|==================================================== | 80%
|
|==================================================== | 81%
|
|===================================================== | 81%
|
|===================================================== | 82%
|
|====================================================== | 82%
|
|====================================================== | 83%
|
|====================================================== | 84%
|
|======================================================= | 84%
|
|======================================================= | 85%
|
|======================================================== | 85%
|
|======================================================== | 86%
|
|======================================================== | 87%
|
|========================================================= | 87%
|
|========================================================= | 88%
|
|========================================================== | 89%
|
|========================================================== | 90%
|
|=========================================================== | 90%
|
|=========================================================== | 91%
|
|============================================================ | 92%
|
|============================================================ | 93%
|
|============================================================= | 93%
|
|============================================================= | 94%
|
|============================================================= | 95%
|
|============================================================== | 95%
|
|============================================================== | 96%
|
|=============================================================== | 96%
|
|=============================================================== | 97%
|
|=============================================================== | 98%
|
|================================================================ | 98%
|
|================================================================ | 99%
|
|=================================================================| 99%
|
|=================================================================| 100%
library (leaflet)
# Joining the flu rate info with the shapefile
pa_flu_rate_merged <- geo_join(counties,pa_flu_rate,"NAME","County")
#Creating a color palette
pal<- colorNumeric("Blues", domain= pa_flu_rate_merged$per_capita)
pa_flu_rate_merged <- subset(pa_flu_rate_merged, !is.na(Total_flu_cases))
#Creating a popup for the map
popup_flu <-paste0("County: ", as.character(pa_flu_rate_merged$County),
"<br> Flu rate: ", as.numeric(pa_flu_rate_merged$per_capita), "<br> Total cases: ",
as.numeric(pa_flu_rate_merged$Total_flu_cases))
# starting to build the map
counties %>%
leaflet() %>%
addTiles() %>%
addPolygons(popup = ~popup_flu)
# Mapping it with the new tiles CartoDB.Positron
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-80.000402, 40.441153, zoom = 6) %>%
addPolygons(data = pa_flu_rate_merged ,
fillColor = ~pal(pa_flu_rate$per_capita),
fillOpacity = 0.7,
weight = 0.2,
smoothFactor = 0.2,
popup = ~popup_flu) %>%
addLegend(pal = pal,
values = pa_flu_rate_merged$per_capita,
position = "bottomright",
title = "Flu rate per 100,000 residents")